/*
 *使用 setChartWidth()
 *send {px 宽度,pageWidth = 默认3840}
 */
export const getCurrentWidth = (px: number, pageWidth = 3840): number | undefined => {
  const clientWidth =
    window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth
  if (!clientWidth) return
  const curWidth = clientWidth / pageWidth
  return px * curWidth
}
/*
 *使用 getCurrentHeight()
 *send {px 宽度,pageWidth = 默认2160}
 */
export const getCurrentHeight = (px: number, pageHeight = 2160): number | undefined => {
  const clientHeight =
    window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
  if (!clientHeight) return
  const fontSize = clientHeight / pageHeight
  return px * fontSize
}
/**
 * @description: 将对象（Object）换成url参数 {a:1,b:2} => 'a=1&b=2'
 * @param {*} T
 * @return {*}
 */
export const objChangeUrl = <T>(params: T): string => {
  const paramsType: string = Object.prototype.toString.call(params).slice(8, -1)
  if (paramsType === 'Object' && Object.keys(params).length) {
    let urlParams: string = ''
    const ObjTypeList: Array<string> = ['[]', '{}', 'null']
    const StringTypeList: Array<string> = ['', '']
    for (const keys in params) {
      if (
        !ObjTypeList.includes(JSON.stringify(params[keys])) &&
        !StringTypeList.includes(params[keys] as unknown as string)
      ) {
        const saveObjParamsType = Object.prototype.toString.call(params[keys]).slice(8, -1)
        const saveValue: any =
          saveObjParamsType === 'Object' || saveObjParamsType === 'Array'
            ? JSON.stringify(params[keys])
            : params[keys]
        urlParams = `${urlParams === '' ? '' : urlParams + '&'}${keys}=${saveValue}`
      }
    }
    return urlParams
  } else {
    console.warn('无效的参数')
    return ''
  }
}
/**
 * @description: url转成对象-如app?a=1&b=2" =》 {a:1,b:2}
 * @param {string} params
 * @return {*}
 */
export const urlToObj = (params: string): Object => {
  if (params.indexOf('?') !== -1) {
    const urlArr: Array<string> = params.split('?')
    const changeStr: string = urlArr[1]
    let curStr = changeStr.replaceAll('=', ':')
    curStr = curStr.replaceAll('&', ',')
    curStr = '{"' + curStr.replaceAll(':', '":"').replaceAll(',', '","') + '"}'
    return JSON.parse(curStr)
  } else {
    console.warn("参数中必须带有'?',如：app?a=1&b=2")
    return {}
  }
}
/**
 * @description: 处理对象空值 {a:1,b:'', c:[],d: {}} => {a:1}
 * @param {*} T
 * @return {Object} {}
 */
export const disObjEmptyValue = async <T>(params: T) => {
  const paramsType: string = Object.prototype.toString.call(params).slice(8, -1)
  if (paramsType === 'Object' && Object.keys(params).length) {
    const ObjTypeList: Array<string> = ['[]', '{}', 'null']
    const StringTypeList: Array<string> = ['', '']
    const valuesList = Object.values(params)
    const keyList = Object.keys(params)
    const resObj: any = {}
    await Promise.all(
      valuesList.map((i: any, k: number) => {
        if (
          !ObjTypeList.includes(JSON.stringify(i)) &&
          !StringTypeList.includes(i) &&
          toString.call(i).slice(8, -1) !== 'Undefined'
        ) {
          resObj[keyList[k] as any] = i
        }
      })
    )
    return resObj
  } else {
    console.warn('参数中必须带是对象')
    return {}
  }
}
